package com.cykj.faceledtv.network;


import android.net.ParseException;

import com.cykj.faceledtv.MyApp;
import com.cykj.faceledtv.R;
import com.google.gson.JsonParseException;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

/**
 * 异常处理
 *
 * @author jzh
 * @date 2018/12/13 10:10
 */
public class ExceptionHandle {
    //token失效
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public static String handleException(Throwable e, boolean isClick) {
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            if (httpException.code() == INTERNAL_SERVER_ERROR || httpException.code() == SERVICE_UNAVAILABLE
                    || httpException.code() == FORBIDDEN) {
                return isClick ? MyApp.getInstance().getString(R.string.serverErrorClick)
                        : MyApp.getInstance().getString(R.string.serverError);
                //需要重新登录app
            } else if (httpException.code() == UNAUTHORIZED) {
                return MyApp.getInstance().getString(R.string.tokenExpired);
            } else if (httpException.code() == NOT_FOUND) {
                return isClick ? MyApp.getInstance().getString(R.string.notFoundErrorClick) :
                        MyApp.getInstance().getString(R.string.notFoundError);
            } else if (httpException.code() == REQUEST_TIMEOUT) {
                return isClick ? MyApp.getInstance().getString(R.string.requestTimeOutErrorClick) :
                        MyApp.getInstance().getString(R.string.requestTimeOutError);
//            } else if (httpException.code() == BAD_GATEWAY) {
//                return isClick ? MyApp.getInstance().getString(R.string.gatewayErrorClick) :
//                        MyApp.getInstance().getString(R.string.gatewayErrorClick);
//            } else if (httpException.code() == GATEWAY_TIMEOUT) {
//                return isClick ? MyApp.getInstance().getString(R.string.gatewayTimeOutErrorClick) :
//                        MyApp.getInstance().getString(R.string.gatewayTimeOutError);
            } else {
                return isClick ? MyApp.getInstance().getString(R.string.networkErrorClick)
                        : MyApp.getInstance().getString(R.string.networkError);
            }
        } else if (e instanceof UnknownHostException) {
            return isClick ? MyApp.getInstance().getString(R.string.networkErrorClick)
                    : MyApp.getInstance().getString(R.string.networkError);
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            return isClick ? MyApp.getInstance().getString(R.string.parseErrorClick)
                    : MyApp.getInstance().getString(R.string.parseError);
        } else if (e instanceof ConnectTimeoutException || e instanceof SocketTimeoutException) {
            return isClick ? MyApp.getInstance().getString(R.string.connectTimeOutClick)
                    : MyApp.getInstance().getString(R.string.connectTimeOut);
        } else {
            return isClick ? MyApp.getInstance().getString(R.string.unknownErrorClick)
                    : MyApp.getInstance().getString(R.string.unknownError);
        }
    }
}
